import { Inject, Controller, Post, Body, Get, Query, Param } from '@midwayjs/core';
import { NewService } from '../../service/news/news.services';
import { News } from '../../entity/news.entity';
import { v4 as uuid } from 'uuid';
import { IResponse } from '../../interface';

@Controller('/api/news')
export class NewsController {
  @Inject()
  newService: NewService;

  @Post('/create')
  async create(@Body() n: News): Promise<News> {
    // 添加 uuid
    n.uuid = uuid();
    return this.newService.create(n);
  }

  /** 查询某一页的所有新闻 */
  @Get('/all')
  async findAll(@Query('pageNo') pageNo: number, @Query('pageSize') pageSize: number): Promise<IResponse<{ data: News[]; total: number }>> {
    return this.newService.findAll(pageNo, pageSize);
  }

  @Get('/detailByType')
  async findByType(@Query('type') type: string, @Query('pageNo') pageNo: number, @Query('pageSize') pageSize: number): Promise<IResponse<{ data: News[]; total: number }>> {
    return this.newService.findByType(type, pageNo, pageSize);
  }

  @Get('/detail/:uuid')
  async findOne(@Param('uuid') uuid: string): Promise<IResponse<{
    data: News,
    last: News,
    next: News,
  }>> {
    return this.newService.findOne(uuid);
  }

  @Post('/update/:uuid')
  async update(@Param('uuid') uuid: string, @Body() n: Partial<News>): Promise<News> {
    return this.newService.update(uuid, n);
  }

  @Post('/remove/:uuid')
  async remove(@Param('uuid') uuid: string): Promise<void> {
    await this.newService.remove(uuid);
  }
}